package com.washmadam.service.controller.cms.customer.order;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.washmadam.service.common.base.BaseController;
import com.washmadam.service.common.base.RequestDTO;
import com.washmadam.service.common.base.ResponseDTO;
import com.washmadam.service.common.constants.Constant;
import com.washmadam.service.common.enums.OrderStatusEnum;
import com.washmadam.service.common.enums.RenewalStatusEnum;
import com.washmadam.service.common.enums.StatusEnum;
import com.washmadam.service.db.dto.CustomerOrder;
import com.washmadam.service.db.request.operate.CustomerOrderOperateParam;
import com.washmadam.service.db.request.query.CustomerOrderQueryParam;
import com.washmadam.service.domain.order.CustomerOrderDomain;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(Constant.URL_PREFIX + "/cms/customer/order")
@Slf4j
public class CustomerOrderCmsController extends BaseController {

	@Autowired
	CustomerOrderDomain customerOrderDomain;
	/**
	 * 
	 * @param 会员订单录入
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addOrder")
	public String addOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【新增订单】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			if(requestParam.getCurrentNum() ==null || requestParam.getCurrentNum() < 1) {
				return ResponseDTO.fail("服务剩余次数不能设置小于1", null);
			}
			buildCreateAndUpdateInfo(requestParam);
			requestParam.setStatus(StatusEnum.ENABLE.getCode());
			customerOrderDomain.saveCustomerOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【新增订单】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	
	@RequestMapping(value = "/renewalOrder")
	public String renewalOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【续费订单】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			if(requestParam.getCurrentNum() ==null || requestParam.getCurrentNum() < 1) {
				return ResponseDTO.fail("服务剩余次数不能设置小于1", null);
			}
			buildCreateAndUpdateInfo(requestParam);
			requestParam.setStatus(StatusEnum.ENABLE.getCode());
			customerOrderDomain.saveRenewalCustomerOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【续费订单】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}

	/**
	 * 
	 * @param 会员订单修改
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editOrder")
	public String editOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【新增订单】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			if(requestParam.getId()==null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			if(requestParam.getCurrentNum() ==null || requestParam.getCurrentNum() < 1) {
				return ResponseDTO.fail("服务剩余次数不能设置小于1", null);
			}
			buildCreateAndUpdateInfo(requestParam);
			requestParam.setStatus(StatusEnum.ENABLE.getCode());
			customerOrderDomain.editCustomerOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【新增订单】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	
	/**
	 * 
	 * @param 订单删除流程
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteOrder")
	public String deleteOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【删除订单】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			buildCreateAndUpdateInfo(requestParam);
			customerOrderDomain.deleteOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【删除订单】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	/**
	 * 
	 * @param 订单退款
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/refundOrder")
	public String refundOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单退款】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			buildCreateAndUpdateInfo(requestParam);
			customerOrderDomain.refundOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【订单退款】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	/**
	 * 
	 * @param 订单预约不提醒
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/notRemindOrder")
	public String notRemindOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【预约订单不提醒】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			buildCreateAndUpdateInfo(requestParam);
			customerOrderDomain.notRemindOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【预约订单不提醒】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	/**
	 * 
	 * @param 订单预约恢复提醒
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/remindOrder")
	public String remindOrder(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【预约订单恢复提醒】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			buildCreateAndUpdateInfo(requestParam);
			customerOrderDomain.remindOrder(requestParam);
			//log.info("【新增订单】调用成功:result;{}", result);
			return ResponseDTO.success();
		} catch (Exception e) {
			log.error("【预约订单恢复提醒】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	
	/**
	 * 
	 * @param 会员订单列表-总清单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getGeneralCustomerOrderList")
	public String getGeneralCustomerOrderList(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单列表-总清单】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderQueryParam.class);
			if(requestParam == null) {
				requestParam= new CustomerOrderQueryParam();
			}
			requestParam.setOrderStatus(OrderStatusEnum.ALL.getCode().toString());
			PageInfo<CustomerOrder> result = customerOrderDomain.getCustomerOrderList(requestParam);
			log.info("【订单列表-总清单】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单列表-总清单】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	/**
	 * 
	 * @param 会员订单列表-待预约
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getWaitingReserveCustomerOrderList")
	public String getWaitingReserveCustomerOrderList(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单列表-待预约】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderQueryParam.class);
			if(requestParam == null) {
				requestParam= new CustomerOrderQueryParam();
			}
			requestParam.setOrderStatus(OrderStatusEnum.WAIT_RESERVE.getCode().toString());
			PageInfo<CustomerOrder> result = customerOrderDomain.getCustomerOrderList(requestParam);
			log.info("【订单列表-待预约】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单列表-待预约】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	@RequestMapping(value = "/getWaitingReserveCustomerOrderListNotRemind")
	public String getWaitingReserveCustomerOrderListNotRemind(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单列表-待预约不提醒】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderQueryParam.class);
			if(requestParam == null) {
				requestParam= new CustomerOrderQueryParam();
			}
			requestParam.setOrderStatus(OrderStatusEnum.WAIT_RESERVE_NOT_REMIND.getCode().toString());
			PageInfo<CustomerOrder> result = customerOrderDomain.getCustomerOrderList(requestParam);
			log.info("【订单列表-不提醒】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单列表-不提醒】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	/**
	 * 
	 * @param 预约异常订单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getErrorReserveCustomerOrderList")
	public String getErrorReserveCustomerOrderList(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单列表-预约异常】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderQueryParam.class);
			if(requestParam == null) {
				requestParam= new CustomerOrderQueryParam();
			}
			requestParam.setOrderStatus(OrderStatusEnum.EXCEPTION_RESERVE.getCode().toString());
			PageInfo<CustomerOrder> result =customerOrderDomain.getCustomerOrderList(requestParam);
			log.info("【订单列表-预约异常】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单列表-预约异常】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	/**
	 * 
	 * @param 待服务订单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getWaitServedCustomerOrderList")
	public String getWaitServedCustomerOrderList(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单列表-待服务】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderQueryParam.class);
			if(requestParam == null) {
				requestParam= new CustomerOrderQueryParam();
			}
			requestParam.setOrderStatus(OrderStatusEnum.COMING_SOON.getCode().toString());
			PageInfo<CustomerOrder> result = customerOrderDomain.getCustomerOrderList(requestParam);
			log.info("【订单列表-待服务】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单列表-待服务】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	
	/**
	 * 
	 * @param 会员过期订单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getExpireCustomerOrderList")
	public String getExpireCustomerOrderList(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单列表-过期】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderQueryParam.class);
			if(requestParam == null) {
				requestParam= new CustomerOrderQueryParam();
			}
			requestParam.setOrderStatus(OrderStatusEnum.MEMBER_EXPIRE.getCode().toString());
			if(requestParam.getRenewalStatus() == null) {
				requestParam.setRenewalStatus(RenewalStatusEnum.INIT.getCode());
			}
			PageInfo<CustomerOrder> result = customerOrderDomain.getCustomerOrderList(requestParam);
			log.info("【订单列表-过期】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单列表-过期】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
	
	/**
	 * 
	 * @param 会员订单详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCustomerOrderInfo")
	public String getCustomerOrderInfo(@RequestBody RequestDTO req, HttpServletRequest request) {
		try {
			log.info("【订单详情】请求参数 data:{}", req.getData());
			if (req.getData() == null) {
				return ResponseDTO.fail("参数缺失", null);
			}
			CustomerOrderOperateParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
					CustomerOrderOperateParam.class);
			CustomerOrder result = customerOrderDomain.getCustomerOrderInfo(requestParam.getId());
			log.info("【订单详情】调用成功:result;{}", result);
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("【订单详情】异常，{}", e);
			return ResponseDTO.fail("请求异常:" + e.getMessage(), null);
		}
	}
}
